/*
 * CoverFlowNodes.fx
 *
 * Created on Mar 29, 2010, 2:25:56 PM
 */

package fxgreader.components;
import javafx.util.Math;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import fxgreader.components.CoverFlowNode;
import java.util.ArrayList;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Polygon;
import javafx.scene.Cursor;
import fxgreader.components.CoverFlowNode;

/**
 * @author perkintang
 */

public class CoverFlow2 extends CustomNode{
public var maxWidth : Number = 600 on replace{
    delete rightButton from group.content;
    rightButton = Polygon {
            points: [maxWidth,(nodeH+10)/2,maxWidth-17,(nodeH+10)/2-20/2,maxWidth-17,(nodeH+10)/2+20/2]
            fill : Color.WHITE
            cursor:Cursor.HAND
            onMouseClicked : function(even :MouseEvent):Void{
                rightClick();
            }
        };
   insert rightButton into group.content;
};
public var nodes : CoverFlowNode[];
public var nodeList : ArrayList = new ArrayList(100);
public var nodeW : Number;
public var nodeH : Number;
public var lazyLoad : function():Void;
var xoffset:Number = bind nodeW+20;
var yoffset:Number = 10;
public var centerIndex : Integer = 0;
public function newNodesAdded(){
//    println("display new nodes : centerIndex at {centerIndex} and nodeList size is{nodeList.size()}");
    var tmp : CoverFlowNode ;
    for(x in [0 .. nodeList.size()-1]){
            tmp = nodeList.get(x) as CoverFlowNode;
            if(x > centerIndex){
                tmp.offsetx_to = (nodeW) + xoffset*Math.abs(x-centerIndex) ;
                tmp.ori_width_to = nodeW;
                tmp.ori_height_to = nodeH;
                tmp.offsety_to = yoffset;
                tmp.duh_to =0;
                tmp.dlh_to = 0;
                tmp.dw_to = 0;
                tmp.quickMove(false);
                tmp.scalrate_to = 1;
                tmp.toFront();
                if((x - centerIndex ) <= 2){
                        tmp.load();
                }
                if((x - centerIndex) > 2){
                        tmp.release();
                }
            }
    }
}
function leftClick():Void{
//    println("parent width : {parent.boundsInLocal.width}");
    var tmp : CoverFlowNode;
    for(node in nodeList){
        tmp = node as CoverFlowNode;
        tmp.offsetx_to = tmp.offsetX + xoffset;
        tmp.move();
    }
    checkForButton(maxWidth);
}
function rightClick():Void{
    var tmp : CoverFlowNode;
    for(node in nodeList){
        tmp = node as CoverFlowNode;
        tmp.offsetx_to = tmp.offsetX - xoffset;
        tmp.move();
    }
    checkForButton(maxWidth);
}
function checkForButton(max : Number):Void{
    var firstNode = nodeList.get(0) as CoverFlowNode;
    if(firstNode.offsetx_to >= 17+10){
        leftButton.visible = false;
    }else{
        leftButton.visible = true;
    }
    var lastNode = nodeList.get(nodeList.size() -1) as CoverFlowNode;
    if(lastNode.offsetx_to <= max - 10 - 17 - nodeW){
        rightButton.visible = false;
    }else{
        rightButton.visible = true;
    }
}
var group : Group;
var nodesGroup : Group;
var background = Rectangle {
                        effect: Reflection { fraction: 0.3 }
                        stroke : Color.WHITE
                        fill : Color.WHITE
//                        translateX: -20
//                        translateY: -20
                        width:  bind 220  height:  bind 220
                 };
var upline : Path = Path {
    stroke:Color.WHITE
    elements: [
        MoveTo { x: 0  y: 10 },
        HLineTo { x: 800 },
    ]
};
public var leftButton : Polygon = Polygon {
    points: [0,(nodeH+10)/2,17,(nodeH+10)/2-20/2,17,(nodeH+10)/2+20/2]
    fill : Color.WHITE
    cursor:Cursor.HAND
    onMouseClicked : function(even :MouseEvent):Void{
        leftClick();
    }
}
public var rightButton : Polygon;
var cy : Rectangle = Rectangle {
    translateX : 17+10
    width: bind maxWidth - (17+10)*2  height: nodeH + yoffset*2
}

override function create():Node {
        nodesGroup = Group{content : nodes clip:cy};
        group = Group{
            visible:bind visible
            onMouseWheelMoved : function(event:MouseEvent):Void{
                    event.wheelRotation
            }
            content : [leftButton,nodesGroup]
        };
        for(index in [0 .. sizeof nodes -1]){
            nodes[index].indexOfpositionerArray = index;
            nodes[index].ori_height = nodeH;
            nodes[index].ori_width = nodeW;
            nodes[index].offsetX = xoffset*index + 17+10;
            nodes[index].offsetY = yoffset;
            nodes[index].dw = 0;
            nodes[index].dlh = 0;
            nodes[index].duh = 0;
            nodes[index].ori_height_to = nodeH;
            nodes[index].ori_width_to = nodeW;
            nodes[index].offsetx_to = xoffset*index;
            nodes[index].offsety_to = yoffset;
            nodes[index].dw_to = 0;
            nodes[index].dlh_to = 0;
            nodes[index].duh_to = 0;
//            nodes[index].background.stroke = null;
            nodes[index].onMouseEntered = function(event : MouseEvent):Void{
                       nodes[index].background.stroke = Color.BLUE;
                };
            nodes[index].onMouseExited = function(event : MouseEvent):Void{
                       nodes[index].background.stroke = Color.WHITE;
                }
            nodes[index].toBack();
            nodeList.add(nodes[index]);
            if(index < 2){
                nodes[index].load();
            }
        };
        return group;
}
function triggerButton(index : Integer,max : Number,node : CoverFlowNode){
                    //the first one
                    if(index == 0){
                       if(node.offsetx_to >= 17+10){
                            leftButton.visible = false;
                       }else{
                            leftButton.visible = true;
                       }
                    }
                    //the last one
                    if(index == nodeList.size() -1){
                       if(node.offsetx_to <= max - 10 - 17){
                            rightButton.visible = false;
                       }else{
                            rightButton.visible = true;
                       }
                    }
            };
public function addNode(node : CoverFlowNode){
            var index = nodeList.size();
            node.indexOfpositionerArray = index;
            node.ori_height = nodeH;
            node.ori_width = nodeW;
            node.offsetX = xoffset*index + 17+10;
            node.offsetY = yoffset;
            node.dw = 0;
            node.dlh = 0;
            node.duh = 0;
            node.ori_height_to = nodeH;
            node.ori_width_to = nodeW;
            node.offsetx_to = xoffset*index;
            node.offsety_to = yoffset;
            node.dw_to = 0;
            node.dlh_to = 0;
            node.duh_to = 0;
            node.toBack();
            node.afterMove = triggerButton;
            nodeList.add(node);
            insert node into nodesGroup.content;
}
public function getNode(index : Integer):CoverFlowNode{
        return nodeList.get(index) as CoverFlowNode;
}
public function clearNode(){
    for(node in nodeList){
        delete (node as CoverFlowNode) from nodesGroup.content;
    }
    nodeList.clear();
}
public function show(){
//    translateY = -(nodeH+10);
//    Timeline {
//    repeatCount: 1
//    keyFrames : [
//    at(0.2s) {
//        translateY => 0 tween Interpolator.EASEOUT;
//    }
//    ]
//    }.playFromStart();
    toFront();
    for(item in nodeList){
          if((item as CoverFlowNode).visible){
            visible = true;
            return;
          }
    }
}
}
